package servlet1;

import dao.DB;
import entity.UserLogin;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/registerServlet")
public class registerServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String acc = req.getParameter("account");
        String pwd = req.getParameter("password");
        String pwd2 = req.getParameter("password2");


       int r = DB.executeUpdate(" INSERT INTO `user` (`id`, `account`, `password`) VALUES (null, ?, ?);",acc,pwd);

        UserService Userser =new UserService();
        // 把参数封装到user对象中
        UserLogin user = new UserLogin();
        user.setAccount(acc);
        user.setPassword(pwd);


         //调用用户业务注册账号，注册成功就去login页，注册失败就去register页
        if (Userser.register(user) == null) {
            req.getSession().setAttribute("msg", "注册失败");
            resp.sendRedirect("register.jsp");
        } else {
            req.getSession().setAttribute("msg", "注册成功");
            resp.sendRedirect("login.jsp");
        }
    }
}
